<?php

/**
 * This is the model class for table "{{transactionsalespurchase}}".
 *
 * The followings are the available columns in table '{{transactionsalespurchase}}':
 * @property string $idtransactionsalespurchase
 * @property integer $idtransactionmaster
 * @property string $idstockitems
 * @property string $transactionsalespurchasequantity
 * @property string $transactionsalespurchaserate
 * @property string $transactionsalespurchasetotalamt
 *
 * The followings are the available model relations:
 * @property Transactionmaster $idtransactionmaster0
 * @property Stockitems $idstockitems0
 */
class Transactionsalespurchase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Transactionsalespurchase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{transactionsalespurchase}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idtransactionmaster', 'numerical', 'integerOnly'=>true),
			array('idstockitems, transactionsalespurchasequantity, transactionsalespurchaserate', 'length', 'max'=>10),
			array('transactionsalespurchasetotalamt', 'length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idtransactionsalespurchase, idtransactionmaster, idstockitems, transactionsalespurchasequantity, transactionsalespurchaserate, transactionsalespurchasetotalamt', 'safe', 'on'=>'search'),
                        array('transactionmastervoucherdate', 'autocompleteValidator', 'model'=>'Transactionmaster'),
                        array('stockitemsname', 'autocompleteValidator', 'model'=>'Stockitems'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!new $params['model']->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['transactionmastervoucherdate'])){
                $match = addcslashes($_REQUEST['transactionmastervoucherdate'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'transactionmastervoucherdate = :transactionmastervoucherdate',
                    'params'    => array(':transactionmastervoucherdate' => "$match")
                ) );            
                $model=Transactionmaster::model()->find($criteria);
                $this->idtransactionmaster = $model->idtransactionmaster;
            }
                        if(isset($_REQUEST['stockitemsname'])){
                $match = addcslashes($_REQUEST['stockitemsname'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'stockitemsname = :stockitemsname',
                    'params'    => array(':stockitemsname' => "$match")
                ) );            
                $model=Stockitems::model()->find($criteria);
                $this->idstockitems = $model->idstockitems;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idtransactionmaster0' => array(self::BELONGS_TO, 'Transactionmaster', 'idtransactionmaster'),
			'idstockitems0' => array(self::BELONGS_TO, 'Stockitems', 'idstockitems'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idtransactionsalespurchase' => Yii::t('application', 'Idtransactionsalespurchase'),
			'idtransactionmaster' => Yii::t('application', 'Idtransactionmaster'),
			'idstockitems' => Yii::t('application', 'Idstockitems'),
			'transactionsalespurchasequantity' => Yii::t('application', 'Transactionsalespurchasequantity'),
			'transactionsalespurchaserate' => Yii::t('application', 'Transactionsalespurchaserate'),
			'transactionsalespurchasetotalamt' => Yii::t('application', 'Transactionsalespurchasetotalamt'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idtransactionsalespurchase',$this->idtransactionsalespurchase,true);
		$criteria->compare('idtransactionmaster',$this->idtransactionmaster);
		$criteria->compare('idstockitems',$this->idstockitems,true);
		$criteria->compare('transactionsalespurchasequantity',$this->transactionsalespurchasequantity,true);
		$criteria->compare('transactionsalespurchaserate',$this->transactionsalespurchaserate,true);
		$criteria->compare('transactionsalespurchasetotalamt',$this->transactionsalespurchasetotalamt,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}